<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Service\ColumnService;
use Hyperf\Validation\Rule;
use Hyperf\DbConnection\Db;
Use Hyperf\Di\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;


class NavColumnController extends AbstractController
{

    /**
     * @Inject
     * @var ColumnService
     *
     */
    private $columnService;

    public function list()
    {
        // 关键字：名称
        $params = $this->request->inputs(['is_show','type']);
        $this->validation($params,[
            'type'=>'between:1,5',
        ]);
        $area_id = $this->request->input('area_id', '');
        $list = $this->paginate($this->columnService->get_list($params));
        return $this->jsonResponse->success($list, '获取列表成功');
    }


    public function add()
    {
        $params = $this->request->inputs(['name','type','is_show','sort']);
        $this->validation($params,
            [
                'name' => "required|string|max:50|unique:fly_column",
                'type' => 'required|integer|between:1,5|unique:fly_column',
                'sort' => 'integer',
                'is_show' => "required|boolean",
            ],
        );
        $params['created_at'] = date('Y-m-d H:i:s');
        $params['create_by'] = parent::getAdminId();;
        try{
            Db::beginTransaction();
            $this->columnService->insert($params);
            Db::commit();
            return $this->jsonResponse->success([],'添加成功');
        } catch (\Throwable $exception) {
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'添加失败'.$exception->getMessage());
        }
    }

    public function edit()
    {
        $params = $this->request->inputs(['id','name','type','is_show','sort']);
        $this->validation($params,
            [
                'id' => [
                    "required",
                    "integer",
                    "min:1",
                    Rule::exists('fly_column','id')->whereNull('deleted_at'),
                ],
                'name' =>[
                    "required",
                    "string",
                    "max:50",
                    Rule::unique('fly_column','id')->whereNull('deleted_at')->ignore($params['id'])
                ],
                'type' =>[
                    "required",
                    "integer",
                    "between:1,5",
                    Rule::unique('fly_column','id')->whereNull('deleted_at')->ignore($params['id'])
                ],
                'sort' => 'integer',
                'is_show' => "required|boolean",
            ],
        );
        $params['updated_at'] = date('Y-m-d H:i:s');
        $params['update_by'] = parent::getAdminId();
        try{
            Db::beginTransaction();
            $this->columnService->update($params);
            Db::commit();
            return $this->jsonResponse->success([],'修改成功');
        } catch (\Throwable $exception) {
            Db::rollBack();
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'修改失败'.$exception->getMessage());
        }
    }

    public function detail()
    {
        $id = $this->request->input('id');
        $this->validation(
            ['id'=>$id],
            [
                'id' => [
                    'required',
                    'integer',
                    'min:0',
                    Rule::exists('fly_column','id')->whereNull('deleted_at'),
                ],
            ],
            );
        try{
            return $this->jsonResponse->success($this->columnService->info(intval($id)),'信息返回');
        } catch (\Throwable $exception) {
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'不存在或已删除：'.$exception->getMessage());
        }

    }


    public function del()
    {
        $ids = $this->request->input('ids');
        $this->validation(
            ['ids'=>$ids],
            [
                'ids' => 'required|array',
                'ids.*' => [
                    'required',
                    'integer',
                    'min:0',
                    Rule::exists('fly_column','id')->whereNull('deleted_at'),
//                     //需要验证没有关联的数据表
//                    Rule::unique('fly_landmark','project_id')->where(function ($query) {
//                        $query->where('tenant_id',$tenantId)
//                            ->whereNull('deleted_at');
//                    }),
                ],

            ],
            [
                'ids.*.unique'  =>'有关联数据，不可删除',
            ]

        );
        try{
            $this->columnService->deleteByUser($ids,$this->getAdminId(),5);
            return $this->jsonResponse->success([],'删除成功');
        } catch (\Throwable $exception) {
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'删除失败');
        }

    }

    public function change_show()
    {
        $ids = $this->request->input('ids');
        $is_show = $this->request->input('is_show');
        $this->validation(
            [
                'ids'=>$ids,
                'is_show'=>$is_show
            ],
            [
                'is_show' => 'required|boolean',
                'ids' => 'required|array',
                'ids.*' => [
                    'required',
                    'integer',
                    'min:0',
                    Rule::exists('fly_column','id')->whereNull('deleted_at')
                ],
            ],
            );
        try{
            $this->columnService->change_show($ids,boolval($is_show),$this->getAdminId());
            return $this->jsonResponse->success([],'修改成功');
        } catch (\Throwable $exception) {
            return $this->jsonResponse->error(ErrorCode::SYSTEM_INVALID,'修改失败:'.$exception->getMessage());
        }

    }


}